package com.mengya.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.DateFormat;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.mengya.adapter.ImageAdapter;
import com.mengya.gzcwt.R;

public class MainActivity extends BaseActivity {
	private PopupWindow popup, popup1;
	private View popupView, popupView1;
	private TextView mTime = null, wifi = null, user = null;
	private final int msgKey1 = 1;
	private String netstate = "";

	public static Context mainContext;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home_bin);
		mainContext = this;
		mTime = (TextView) findViewById(R.id.time);
		wifi = (TextView) findViewById(R.id.wifi);
		user = (TextView) findViewById(R.id.user);

		String username = getResources().getString(R.string.username);
		user.setText(String.format(username, sp.getString("user", "王良")));
		new TimeThread().start();
		new CheckNetThread().start();

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		netstate = checkNetworkInfo();
		if (!netstate.startsWith("OK,W")) {
			wifi.setTextColor(this.getResources().getColor(R.color.red));
			wifi.setText(getString(R.string.wifi_error));
		} else {
			wifi.setTextColor(this.getResources().getColor(R.color.green));
			wifi.setText(getString(R.string.wifi_ok));

		}
	}

	public class TimeThread extends Thread {
		@Override
		public void run() {
			do {
				try {
					Thread.sleep(1000);
					Message msg = new Message();
					msg.what = msgKey1;
					mHandler.sendMessage(msg);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			} while (true);
		}
	}

	public class CheckNetThread extends Thread {
		@Override
		public void run() {
			do {
				try {
					Thread.sleep(5000);
					netstate = checkNetworkInfo();
					if (!netstate.startsWith("OK,W")) {
						// wifi.setText(getString(R.string.wifi_error));
						sp.edit().putBoolean("is_network_ok", false).commit();
					} else {
						// wifi.setText(getString(R.string.wifi_ok));
						sp.edit().putBoolean("is_network_ok", true).commit();
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			} while (true);
		}
	}

	public String checkNetworkInfo() {
		ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); // mobile

		android.net.NetworkInfo.State wifi = conMan.getNetworkInfo(
				ConnectivityManager.TYPE_WIFI).getState();
		if (wifi.toString().equals("CONNECTED")) {
			netstate = "OK,W";
		}
		return netstate;
	}

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case msgKey1:
				long sysTime = System.currentTimeMillis();
				CharSequence sysTimeStr = DateFormat.format(
						"yyyy-MM-dd hh:mm:ss", sysTime);

				mTime.setText(sysTimeStr);
				break;

			default:
				break;
			}
		}
	};

	public void onClick_menu(View v) {
		switch (v.getId()) {
		case R.id.m1:
			Toast.makeText(MainActivity.this, getString(R.string.setting),
					Toast.LENGTH_SHORT).show();
			break;
		case R.id.m2:
			Toast.makeText(MainActivity.this, getString(R.string.update),
					Toast.LENGTH_SHORT).show();
			break;
		case R.id.m3:
			Toast.makeText(MainActivity.this, getString(R.string.userinfo),
					Toast.LENGTH_SHORT).show();
			break;
		case R.id.m4:
			Toast.makeText(MainActivity.this, getString(R.string.more),
					Toast.LENGTH_SHORT).show();
			break;
		}
		// Tj7Cq3HB4bG3
	}

	public void onBackPressed() {
		// super.onBackPressed();
	}

	public void onClick(View v) {

		popupView = LayoutInflater.from(this).inflate(R.layout.view, null);
		popup = new PopupWindow(popupView, LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT);

		popup.setOutsideTouchable(true);

		popupView.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					popup.dismiss();
				}
				return false;
			}
		});

		popup.setFocusable(true);
		popup.setBackgroundDrawable(new BitmapDrawable());

		popup.setTouchable(true);
		GridView gridview = (GridView) popupView.findViewById(R.id.gridview);
		gridview.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				int d = position + 1;
				switch (d) {
				case 1:
					showToast("打开站场图");
					// Toast.makeText(MainActivity.this,
					// "你选择了" + (position + 1) + " 号图片", Toast.LENGTH_SHORT)
					// .show();
					break;
				case 2:
					showToast("学习资料");
					break;
				case 3:
					showToast("视频文件");
					break;
				case 4:
					showToast("中心简报");
					break;
				case 5:
					showToast("规章制度");
					break;
				case 6:
					showToast("行业规则");
					break;
				}

			}
		});
		// 取得GridView对象
		// 添加元素给gridview
		ImageAdapter adapter = new ImageAdapter(this);
		// gridview.setAdapter(new ImageAdapter(this));
		gridview.setAdapter(adapter);
		// 设置Gallery的背景
		// gridview.setBackgroundResource(R.drawable.bg0);

		ImageView image = null;
		switch (v.getId()) {
		case R.id.linearLayout1:
			// popupView.findViewById(R.id.linear1).setVisibility(View.VISIBLE);
			// image = (ImageView) popupView.findViewById(R.id.imageView1);
			// image.setImageResource(R.drawable.icon1_1);
			Intent intent = new Intent(this, NoticeActivity.class);
			startActivity(intent);
			break;
		case R.id.linearLayout2:
			popupView.findViewById(R.id.linear2).setVisibility(View.VISIBLE);
			image = (ImageView) popupView.findViewById(R.id.imageView2);
			image.setImageResource(R.drawable.icon2_1);

			int[] location = new int[2];
			v.getLocationOnScreen(location);
			int x = location[0];
			int y = location[1];
			System.out.println("x:" + x + "y:" + y);
			popup.showAtLocation(popupView, Gravity.TOP, 0, location[1]);
			break;
		case R.id.linearLayout3:
			// popupView.findViewById(R.id.linear3).setVisibility(View.VISIBLE);
			// image = (ImageView) popupView.findViewById(R.id.imageView3);
			// image.setImageResource(R.drawable.icon3_1);
			showToast(getString(R.string.notopen));
			break;
		case R.id.linearLayout4:
			showToast(getString(R.string.notopen));
			// popupView.findViewById(R.id.linear4).setVisibility(View.GONE);
			// image = (ImageView) popupView.findViewById(R.id.imageView4);
			// image.setImageResource(R.drawable.icon4_1);
			break;
		case R.id.linearLayout5:
			// popupView.findViewById(R.id.linear5).setVisibility(View.GONE);
			// image = (ImageView) popupView.findViewById(R.id.imageView5);
			// image.setImageResource(R.drawable.icon5_1);
			showToast(getString(R.string.notopen));
			break;

		}

	}

	public void onClick_line2(View v) {
		// popupView1 = LayoutInflater.from(this).inflate(R.layout.view2, null);
		// popup1 = new PopupWindow(popupView1, LayoutParams.FILL_PARENT,
		// LayoutParams.WRAP_CONTENT);
		// popup1.setOutsideTouchable(true);
		// popup1.setFocusable(true);
		// popup1.setTouchable(true);
		//
		// popupView1.setOnTouchListener(new OnTouchListener() {
		//
		// @Override
		// public boolean onTouch(View v, MotionEvent event) {
		// if (event.getAction() == MotionEvent.ACTION_DOWN) {
		// popup1.dismiss();
		// }
		// return false;
		// }
		// });
		//
		// // -----
		// popup1.setBackgroundDrawable(new BitmapDrawable());
		// GridView gridview1 = (GridView)
		// popupView1.findViewById(R.id.gridview);
		// gridview1.setOnItemClickListener(new OnItemClickListener() {
		// public void onItemClick(AdapterView<?> parent, View v,
		// int position, long id) {
		// Toast.makeText(MainActivity.this,
		// "你选择了" + (position + 1) + " 号图片", Toast.LENGTH_SHORT)
		// .show();
		// }
		// });
		// // 取得GridView对象
		// // 添加元素给gridview
		// gridview1.setAdapter(new ImageAdapter(this));
		//
		// // 设置Gallery的背景
		// // gridview.setBackgroundResource(R.drawable.bg0);
		//
		// // 事件监听
		// gridview1.setOnItemClickListener(new OnItemClickListener() {
		// public void onItemClick(AdapterView<?> parent, View v,
		// int position, long id) {
		// // Toast.makeText(MainActivity.this,
		// // "你选择了" + (position + 1) + " 号图片", Toast.LENGTH_SHORT)
		// // .show();
		//
		// }
		// });
		// ImageView image = null;
		switch (v.getId()) {
		case R.id.linearLayout6:
			// popupView1.findViewById(R.id.linear1).setVisibility(View.VISIBLE);
			// image = (ImageView) popupView1.findViewById(R.id.imageView1);
			// image.setImageResource(R.drawable.icon6_1);
//			showToast("打开同步更新页面");
			Intent intent = new Intent(this, UpdateActivity.class);
			startActivity(intent);
			break;
		case R.id.linearLayout7:
			// popupView1.findViewById(R.id.linear2).setVisibility(View.VISIBLE);
			// image = (ImageView) popupView1.findViewById(R.id.imageView2);
			// image.setImageResource(R.drawable.icon7_1);
			showToast("快速检索页面");
			break;
		case R.id.linearLayout8:
			// popupView1.findViewById(R.id.linear3).setVisibility(View.VISIBLE);
			// image = (ImageView) popupView1.findViewById(R.id.imageView3);
			// image.setImageResource(R.drawable.icon8_1);
			showToast("打开收藏夹");
			break;
		case R.id.linearLayout9:
			// popupView1.findViewById(R.id.linear4).setVisibility(View.VISIBLE);
			// image = (ImageView) popupView1.findViewById(R.id.imageView4);
			// image.setImageResource(R.drawable.icon9_1);
			showToast("打开书签");
			break;
		case R.id.linearLayout10:
			// popupView1.findViewById(R.id.linear5).setVisibility(View.VISIBLE);
			// image = (ImageView) popupView1.findViewById(R.id.imageView5);
			// image.setImageResource(R.drawable.icon10_1);
			showToast("打开日常工具");
			break;
		}
		// int[] location = new int[2];
		// v.getLocationOnScreen(location);
		// int x = location[0];
		// int y = location[1];
		// System.out.println("lance x:" + x + "y:" + y);
		// popup1.showAtLocation(popupView1, Gravity.TOP, 0, location[1]);

	}
}
